<template>
  <el-dialog width="500px" :title=" deptForm.id ? '更新数据' : '添加数据' " :visible="dialogVisible" @close="close">
    <el-form ref="deptForm" label-width="120px" :model="deptForm" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" size="mini" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门类型" prop="type">
        <el-select v-model="deptForm.type" size="mini">
          <el-option label="分公司" value="分公司" />
          <el-option label="部门" value="部门" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerName">
        <el-select v-model="deptForm.managerName" size="mini">
          <el-option label="张三三" value="张三三" />
          <el-option label="李四四" value="李四四" />
          <el-option label="何笑笑" value="何笑笑" />
          <el-option label="李维" value="李维" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门描述" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" style="width: 80%" :rows="2" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="16">
            <el-button size="mini" type="primary" @click="submit">确定</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartment, updateDepartment, getDepartmentDetail } from '@/api/homework/zhuzhijiagou'
export default {
  name: 'AddDept',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      deptForm: {
        name: '',
        type: '',
        managerName: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '部门长度在2-8之间', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '部门类型不能为空', trigger: 'blur' }
        ],
        managerName: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门描述为空', trigger: 'blur' },
          { min: 1, max: 18, message: '部门描述长度在1-18之间', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    close() {
      this.$emit('close')
      // 表单重置
      this.$refs.deptForm.resetFields()
      // 手动重置
      this.deptForm = {}
    },
    // 提交表单
    submit() {
      // 兜底校验
      this.$refs.deptForm.validate(async(valid) => {
        if (!valid) return false
        if (this.deptForm.id) {
          await updateDepartment(this.deptForm)
        } else {
          await addDepartment(this.deptForm)
        }
        this.$message.success(`${this.deptForm.id ? '更新成功' : '添加成功'}`)
        this.close()
        this.$emit('update')
      })
    },
    // 数据回显
    async getDetail(id) {
      const res = await getDepartmentDetail(id)
      this.deptForm = res.data
    }
  }

}

</script>

<style>

</style>
